home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Python / dynload_shlib.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-03  |  1.9 KB  |  91 lines

  1. /* Support for dynamic loading of extension modules */
  2.  
  3. #include "Python.h"
  4. #include "importdl.h"
  5.  
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #if defined(__NetBSD__) && (NetBSD < 199712)
  9. #include <nlist.h>
  10. #include <link.h>
  11. #define dlerror() "error in dynamic linking"
  12. #else
  13. #ifdef HAVE_DLFCN_H
  14. #include <dlfcn.h>
  15. #endif
  16. #endif
  17.  
  18. #ifndef RTLD_LAZY
  19. #define RTLD_LAZY 1
  20. #endif
  21.  
  22.  
  23. const struct filedescr _PyImport_DynLoadFiletab[] = {
  24.     {".so", "rb", C_EXTENSION},
  25.     {"module.so", "rb", C_EXTENSION},
  26.     {0, 0}
  27. };
  28.  
  29. static struct {
  30.     dev_t dev;
  31.     ino_t ino;
  32.     void *handle;
  33. } handles[128];
  34. static int nhandles = 0;
  35.  
  36.  
  37. dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
  38.                     const char *pathname, FILE *fp)
  39. {
  40.     dl_funcptr p;
  41.     void *handle;
  42.     char funcname[258];
  43.     char pathbuf[260];
  44.  
  45.     if (strchr(pathname, '/') == NULL) {
  46.         /* Prefix bare filename with "./" */
  47.         sprintf(pathbuf, "./%-.255s", pathname);
  48.         pathname = pathbuf;
  49.     }
  50.  
  51.     /* ### should there be a leading underscore for some platforms? */
  52.     sprintf(funcname, "init%.200s", shortname);
  53.  
  54.     if (fp != NULL) {
  55.         int i;
  56.         struct stat statb;
  57.         fstat(fileno(fp), &statb);
  58.         for (i = 0; i < nhandles; i++) {
  59.             if (statb.st_dev == handles[i].dev &&
  60.                 statb.st_ino == handles[i].ino) {
  61.                 p = (dl_funcptr) dlsym(handles[i].handle,
  62.                                funcname);
  63.                 return p;
  64.             }
  65.         }
  66.         if (nhandles < 128) {
  67.             handles[nhandles].dev = statb.st_dev;
  68.             handles[nhandles].ino = statb.st_ino;
  69.         }
  70.     }
  71.  
  72. #ifdef RTLD_NOW
  73.     /* RTLD_NOW: resolve externals now
  74.        (i.e. core dump now if some are missing) */
  75.     handle = dlopen(pathname, RTLD_NOW);
  76. #else
  77.     if (Py_VerboseFlag)
  78.         printf("dlopen(\"%s\", %d);\n", pathname,
  79.                RTLD_LAZY);
  80.     handle = dlopen(pathname, RTLD_LAZY);
  81. #endif /* RTLD_NOW */
  82.     if (handle == NULL) {
  83.         PyErr_SetString(PyExc_ImportError, dlerror());
  84.         return NULL;
  85.     }
  86.     if (fp != NULL && nhandles < 128)
  87.         handles[nhandles++].handle = handle;
  88.     p = (dl_funcptr) dlsym(handle, funcname);
  89.     return p;
  90. }
  91.